<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
<!--      <el-form-item label="">-->
<!--        <el-button size="small" type="warning" @click="handleBatchDel">批量删除</el-button>-->
<!--      </el-form-item><br>-->

<!--      <el-form-item label="设备id">-->
<!--        <el-input size="small" clearable v-model="formInline.deviceId" placeholder="输入设备id"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="设备名称">
        <el-input size="small" clearable v-model="formInline.deviceName" placeholder="输入设备名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">新增提报</el-button>



      </el-form-item><br>
<!--      <el-form-item label="项目编号">-->
<!--        <el-input size="small" clearable v-model="formInline.deviceNumber" placeholder="输入项目编号"></el-input>-->
<!--      </el-form-item>-->
      <!--      <el-option label="状态" value="status">-->
      <!--        <el-select v-model="formInline.status" placeholder="请选择">-->
      <!--          <el-option label="正常" value="1"></el-option>-->
      <!--          <el-option label="故障" value="2"></el-option>-->
      <!--          <el-option label="维修" value="3"></el-option>-->
      <!--          <el-option label="报废" value="4"></el-option>-->
      <!--        </el-select>-->
      <!--      </el-option>-->
      <el-form-item label="安装地点">
        <el-input size="small" clearable v-model="formInline.installationLocation" placeholder="输入安装地点"></el-input>
      </el-form-item><br>
      <!--      <el-form-item label="提报时间">-->
      <!--        <el-date-picker-->
      <!--          v-model="value1"-->
      <!--          type="daterange"-->
      <!--          range-separator="至"-->
      <!--          start-placeholder="开始"-->
      <!--          end-placeholder="结束">-->
      <!--          <template slot-scope="scope">-->
      <!--            <div>{{ scope.row.createTime | timestampToTime}}</div>-->
      <!--          </template>-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item><br>-->
      <!--      <el-form-item label="呼叫时间">-->
      <!--        <el-date-picker-->
      <!--          v-model="value1"-->
      <!--          type="daterange"-->
      <!--          range-separator="至"-->
      <!--          start-placeholder="开始"-->
      <!--          end-placeholder="结束">-->
      <!--          <template slot-scope="scope">-->
      <!--            <div>{{ scope.row.createTime | timestampToTime}}</div>-->
      <!--          </template>-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item><br>-->

      <!--      <el-form-item>-->
      <!--        <el-button size="small" type="primary" icon="el-icon-search" @click="search">查询</el-button>-->
      <!--        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">新增提报</el-button>-->


      <!--        <el-button size="small" type="primary" icon="el-icon-plus" @click="exportExcel()">导出Excel</el-button>-->

      <!--        <el-button size="small" type="primary" icon="el-icon-plus" @click="importvis=true">导入Excel</el-button>-->
      <!--      </el-form-item>-->



    </el-form>
    <!--列表

        @selection-change="handleSelectionChange"  表示只要我們点击选中当前行，就会回调 handleSelectionChange方法
                        最用作用：就是多选删除
        :data="listData"   绑定数组数据
         v-loading="loading" 表示table数据在加载时的状态
    -->
    <el-table size="small" @selection-change="handleSelectionChange" :data="listData" highlight-current-row
              v-loading="loading" border element-loading-text="玩命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="deviceId" label="设备ID">
      </el-table-column>
      <el-table-column sortable prop="deviceNumber" label="项目编号">
      </el-table-column>
      <el-table-column sortable prop="deviceName" label="设备名称">
      </el-table-column>
      <el-table-column sortable prop="deviceFaultcote" label="故障标题">
      </el-table-column>
      <!--      <el-table-column sortable prop="deviceCall" label="呼叫时间">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column sortable prop="deviceRep" label="提报时间">-->
      <!--      </el-table-column>-->
      <el-table-column sortable prop="deviceCall" label="呼叫时间">
        <template slot-scope="scope">
          <div>{{ scope.row.deviceCall | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column sortable prop="deviceRep" label="提报时间">
        <template slot-scope="scope">
          <div>{{ scope.row.deviceRep | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column sortable prop="installationLocation" label="安装地点">
      </el-table-column>
      <el-table-column sortable prop="problem" label="问题来源">
      </el-table-column>
      <el-table-column sortable prop="deviceStatus" label="设备状态">
      </el-table-column>
      <el-table-column sortable prop="deviceUrgency" label="紧急程度">
      </el-table-column>
      <el-table-column sortable prop="deviceRepname" label="提报人">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="Devicedelete(scope.row.deviceId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 分页组件
     @callFather="callFather"  每当点击组件时 分页发起请求
    -->
    <Pagination v-bind:child-msg="pageparm" v-model="pageparm" @callFather="callFather"></Pagination>

    <!-- 添加/编辑界面
      :model="editForm"  绑定表单数据
       :rules="rules"    绑定数据校验规则
    -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="设备编号" prop="deviceNumber">
          <el-input size="small" v-model="editForm.deviceNumber" auto-complete="off" placeholder="请输入设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input size="small" v-model="editForm.deviceName" auto-complete="off" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="故障标题" prop="deviceFaultcote">
          <el-input size="small" v-model="editForm.deviceFaultcote" auto-complete="off" placeholder="请输入故障标题"></el-input>
        </el-form-item>
        <el-form-item label="呼叫时间" prop="deviceCall">
          <el-input size="small" v-model="editForm.deviceCall" auto-complete="off" placeholder="请选择呼叫时间">
            <!-- <div>{{ scope.row.createTime | timestampToTime }}</div>-->
            <template slot-scope="scope">
              <div>{{ scope.row.createTime | timestampToTime }}</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="安装地点" prop="installationLocation">
          <el-input size="small" v-model="editForm.installationLocation" auto-complete="off" placeholder="请输入安装地点"></el-input>
        </el-form-item>
        <el-form-item label="问题来源" prop="problem">
          <el-input size="small" v-model="editForm.problem" auto-complete="off" placeholder="请输入问题来源"></el-input>
        </el-form-item>

        <el-form-item label="设备状态" prop="deviceStatus">
          <el-input size="small" v-model="editForm.deviceStatus" auto-complete="off" placeholder="请输入员工状态"></el-input>
        </el-form-item>
        <el-form-item label="紧急程度" prop="deviceUrgency">
          <el-input size="small" v-model="editForm.deviceUrgency" auto-complete="off" placeholder="输入紧急程度"></el-input>
        </el-form-item>
        <el-form-item label="提报人" prop="deviceRepname">
          <el-input size="small" v-model="editForm.deviceRepname" auto-complete="off" placeholder="请输入员工名字"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>





  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import {deviceList, deviceSave,deviceEdit, deviceDelete, deviceStatus, deviceBatchDel} from '../../api/fault_magMG'
import axios from "axios";

export default {
  name: "device",
  data() {

    return {
      importvis:false,	  // 控制导入弹框是否显示
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        deviceNumber: '',
        deviceName: '',
        deviceFaultcote: '',
        deviceCall: '',
        deviceRep: '',
        installationLocation: '',
        problem: '',
        deviceStatus: '',
        deviceUrgency: '',
        deviceRepname: '',

      },
      // rules表单验证
      rules: {
        deviceName: [
          {required: true, message: '请输入设备名', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],
        deviceFaultcote: [
          {required: true, message: '请输入标题名', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],

      },
      formInline: {
        pageNum: 1,
        pageSize: 10,

        // deviceId: '',
        deviceName: '',

        installationLocation: '',

      },
      // 删除
      seletedata: {
        ids: '',
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      deviceStatus: true,
      multipleSelection: []
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 创建完毕   初始化数据 最好 在mounted初始
   */
  created() {
    this.getdata(this.formInline)
  },
  methods: {
    getdata(formInline) {
      console.log(formInline)
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      deviceList(formInline)
        .then(res => {  // 后端响应
          this.loading = false
          if (res.code != 20000) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            const pageInfo = res.data;  //先得到pageInfo   (pageHelper 中的分页信息)
            this.listData = pageInfo.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.total = pageInfo.total
            console.log(this.pageparm)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('用户信息加载失败，请稍后再试！')
        })
    },
    editStatus1(currentLineData) {
      // console.log(currentLineData)
      var status = -1;
      if (currentLineData.status == 0) {
        status = 1;
      } else {
        status = 0
      }
      deviceStatus({
        deviceId: currentLineData.deviceId,
        status: status
      }).then(res => {
        if (res.code == 20000) {
          this.$message.success({
            message: '用户状态修改成功',
            duration: 500
          })
          this.getdata(this.formInline)
        }
      })
    },
    callFather(pageparam) {
      this.formInline.pageNum = pageparam.pageNum;
      this.formInline.pageSize = pageparam.pageSize;
      this.getdata(this.formInline)
    },
    search() {
      // console.log(this.formInline)
      this.getdata(this.formInline)
    },
    handleAdd() {
      this.editFormVisible = true;
      this.title = "添加";
      // 表单置空
      this.editForm = {}
    },
    closeDialog() {
      this.editFormVisible = false;
      this.getdata(this.formInline)
    },
    submitForm(editForm) {
      // 点击提交按钮会校验输入框
      this.$refs[editForm].validate((valid) => {
        // 如果校验通过再发请求
        if (valid) {
          if (this.title == "添加") {
            // 实现添加
            deviceSave(this.editForm).then(res => {
              if (res.code = 20000) {
                this.editFormVisible = false;
                this.$message.success({
                  message: '用户添加成功',
                  duration: 500
                })
                this.getdata(this.formInline)
              } else {
                this.$message.error({
                  message: '用户添加失败',
                  duration: 500
                })
              }
            })
          } else if (this.title = "编辑") {
            console.log(this.editForm)
            deviceEdit(this.editForm).then(res => {
              if (res.code = 20000) {
                this.editFormVisible = false;
                this.$message.success({
                  message: '用户更新成功',
                  duration: 500
                })
                this.getdata(this.formInline)
              } else {
                this.$message.error({
                  message: '用户更新失败',
                  duration: 500
                })
              }
            })
          }

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleEdit(index, rowData) {
      // console.log(index,rowData)
      this.title = "编辑"
      this.editForm = rowData;
      this.editFormVisible = true;
    },
    Devicedelete(deciceId) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deviceDelete(deciceId).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getdata(this.formInline)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {
      // val是数组,其中是前端框架自己封装的,其实是勾选的每一行数据对象
      console.log(val)
      var idArr = [];
      for (var i = 0; i < val.length; i++) {
        var obj = val[i];
        //console.log(obj)
        idArr.push(obj.deciceId);
      }
      // console.log(idArr)

      this.multipleSelection = idArr;
    },
    handleBatchDel() {
      this.$confirm('批量删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deviceBatchDel({
          list:this.multipleSelection
        }).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.formInline.pageNum = 1;
            this.getdata(this.formInline);// 重新请求数据
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    exportExcel: function () {
      axios.request({
        url: '/api/faultMaintenance/faultMaintenanceExport',
        method: 'get',
        responseType: 'blob',  // 请求文件
      }).then(ret => {
        this.download(ret, "用户信息")  // 请求导出的响应，需要保存为 学生信息的文件
      })
    },
    // 下载excel的工具方法
    download(res, name) {
      if (!res) {
        return;
      }
      console.log("res", res)
      let url = window.URL.createObjectURL(new Blob([res.data],
        {type: "application/vnd.ms-excel"}));
      let link = document.createElement('a');
      link.style.display = 'none';
      link.href = url;
      link.setAttribute('download', name);
      document.body.appendChild(link);
      link.click();
    },
    upload(res){  // 导入成功数据以后的回调
      if(res.code==20000){
        //成功
        this.importvis=false;
        window.location.reload();//整个界面刷新
      }else {
        this.$message({
          type: 'warning',
          message: '嗨，导入失败！！！！'
        })
      }
    },



  }
}
</script>

<style scoped>

</style>
